package com.example.demo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo.R;
import com.example.demo.entity.Goods;

import java.util.ArrayList;
import java.util.List;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.GoodHolder> {
    private Context context;
    private List<Goods> goodsList ;

    public GoodsAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Goods> goodsList){
        this.goodsList = goodsList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_good, parent, false);
        return new GoodHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodHolder holder, int position) {
        Goods goods = goodsList.get(position);
        holder.img.setImageResource(goods.getImg());
        holder.tvName.setText(goods.getName());
        holder.tvCount.setText(goods.getCount());
        holder.tvA.setText(goods.getA());

    }

    @Override
    public int getItemCount() {

        return goodsList == null ? 0 : goodsList.size();
    }

    public class GoodHolder extends RecyclerView.ViewHolder {

         ImageView img;
         TextView tvName;
         TextView tvCount;
         TextView tvA;
         public GoodHolder(@NonNull View itemView) {
             super(itemView);

             img = itemView.findViewById(R.id.img1);
             tvName = itemView.findViewById(R.id.tv_name);
             tvCount = itemView.findViewById(R.id.tv_count);
             tvA = itemView.findViewById(R.id.tv_a);
         }
     }
}
